#include <QDateTime>
#include <QTimer>
#include <QDebug>
#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);

	m_msgID[eMSGStandby] = RegisterWindowMessage(L"Standby");
	m_msgID[eMSGStandbyDone] = RegisterWindowMessage(L"StandbyDone");
	m_msgID[eMSGReady] = RegisterWindowMessage(L"Ready");
	m_msgID[eMSGReadyDone] = RegisterWindowMessage(L"ReadyDone");
	m_msgID[eMSGEXPOON] = RegisterWindowMessage(L"EXPOON");
	m_msgID[eMSGEXPOOFF] = RegisterWindowMessage(L"EXPOOFF");
	m_msgID[eMSGIMGDone] = RegisterWindowMessage(L"IMGDone");
	m_msgID[eMSGClose] = RegisterWindowMessage(L"Close");
	m_msgID[eMSGError] = RegisterWindowMessage(L"Error");
	for (int var = 0; var < 9; ++var)
	{
		qDebug() << "[register message]" << m_msgID[var];
	}
}

MainWindow::~MainWindow()
{
	delete ui;
}

bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
	MSG *pMSG = (MSG*)message;
	qDebug() << "[event type]" << pMSG->time << pMSG->hwnd << pMSG->lParam << pMSG->wParam << pMSG->message << (LRESULT)result;
	if(pMSG->message == m_msgID[eMSGStandby])
	{
		ui->lsvLog->addItem(QString("[%1] receive Standby").arg(QDateTime::currentDateTime().toString()));
		ui->lsvLog->scrollToBottom();
		QTimer::singleShot(3000, this, SLOT(SlotTimerSendStandbyDone()));
		return true;
	}
	else if(pMSG->message == m_msgID[eMSGReady])
	{
		ui->lsvLog->addItem(QString("[%1] receive Ready").arg(QDateTime::currentDateTime().toString()));
		ui->lsvLog->scrollToBottom();
		QTimer::singleShot(3000, this, SLOT(SlotTimerSendReadyDone()));
		return true;
	}
	else if(pMSG->message == m_msgID[eMSGClose])
	{
		ui->lsvLog->addItem(QString("[%1] receive Close").arg(QDateTime::currentDateTime().toString()));
		ui->lsvLog->scrollToBottom();
		QTimer::singleShot(3000, this, SLOT(SlotTimerClose()));
		return true;
	}
	else return false;
}

void MainWindow::SlotTimerSendStandbyDone()
{
	bool r = PostMessage(HWND_BROADCAST, m_msgID[eMSGStandbyDone], 0, 0);
	ui->lsvLog->addItem(QString("[%1] send StandbyDone(%2)").arg(QDateTime::currentDateTime().toString()).arg(r));
	ui->lsvLog->scrollToBottom();
}

void MainWindow::SlotTimerSendReadyDone()
{
	bool r = PostMessage(HWND_BROADCAST, m_msgID[eMSGReadyDone], 0, 0);
	ui->lsvLog->addItem(QString("[%1] send ReadyDone(%2)").arg(QDateTime::currentDateTime().toString()).arg(r));
	ui->lsvLog->scrollToBottom();
	QTimer::singleShot(3000, this, SLOT(SlotTimerSendEXPOON()));
}

void MainWindow::SlotTimerSendEXPOON()
{
	bool r = PostMessage(HWND_BROADCAST, m_msgID[eMSGEXPOON], 0, 0);
	ui->lsvLog->addItem(QString("[%1] send EXPOON(%2)").arg(QDateTime::currentDateTime().toString()).arg(r));
	ui->lsvLog->scrollToBottom();
	QTimer::singleShot(3000, this, SLOT(SlotTimerSendEXPOOFF()));
}

void MainWindow::SlotTimerSendEXPOOFF()
{
	bool r = PostMessage(HWND_BROADCAST, m_msgID[eMSGEXPOOFF], 0, 0);
	ui->lsvLog->addItem(QString("[%1] send EXPOOFF(%2)").arg(QDateTime::currentDateTime().toString()).arg(r));
	ui->lsvLog->scrollToBottom();
	QTimer::singleShot(3000, this, SLOT(SlotTimerSendIMGDone()));
//	QTimer::singleShot(1000, this, SLOT(SlotTimerError()));
}

void MainWindow::SlotTimerSendIMGDone()
{
	bool r = PostMessage(HWND_BROADCAST, m_msgID[eMSGIMGDone], 0, 0);
	ui->lsvLog->addItem(QString("[%1] send IMGDone(%2)").arg(QDateTime::currentDateTime().toString()).arg(r));
	ui->lsvLog->scrollToBottom();
}

void MainWindow::SlotTimerClose()
{
	this->close();
}

void MainWindow::SlotTimerError()
{
	bool r = PostMessage(HWND_BROADCAST, m_msgID[eMSGError], 0, 0);
	ui->lsvLog->addItem(QString("[%1] send Error(%2)").arg(QDateTime::currentDateTime().toString()).arg(r));
	ui->lsvLog->scrollToBottom();
}
